//
//  ShowBankView.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/10/23.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ShowBankView.h"

#import "JTCSM.h"
#import "MybanKHeaderView.h"


@implementation ShowBankView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame WithImageArray:(NSArray *)array labelTextArray:(NSString *)text{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KVIEWwith, KVIEWheight)];
        self.scrollview.backgroundColor = [UIColor blackColor];
        self.scrollview.userInteractionEnabled = YES;
        self.scrollview.bounces = NO;
        [self addSubview:self.scrollview];
        
        CGFloat width = 16*array.count;
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((KVIEWwith-width)/2, self.scrollview.frame.size.height-20, width, 10)];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = [array count];
        [self addSubview:self.pageControl];
        
        [self viewDidLoadWithImageArray:array title:text];
        self.array = array;
    }
    
    self.backgroundColor = [UIColor whiteColor];
    //    UILabel *label =[UILabel initWithFrame:CGRectMake(0, self.scrollview.frame.size.height-labelHeight, self.scrollview.frame.size.width, labelHeight) text:[NSString stringWithFormat:@"%@次推荐成功",text] font:18 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:RGBACOLOR(255, 68, 0, 1)];
    //    [self addSubview:label];
    
    return self;
}

- (void)viewDidLoadWithImageArray:(NSArray *)array title:(NSString *)text
{
    
    //    图片的宽
    CGFloat imageW = self.scrollview.frame.size.width;
    //    CGFloat imageW = 300;
    //    图片高
    CGFloat imageH = self.scrollview.frame.size.height;
    //    图片的Y
    CGFloat imageY = 0;
    
    
    //   1.添加5张图片
    for (int i = 0; i < array.count; i++) {
        
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
        _imageView.tag = 300+i;
        [df layerWithView:_imageView radius:.1 width:0 color:nil];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        //        图片X
        CGFloat imageX = i * imageW;
        //        设置frame
        _imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        //        设置图片
        NSString *name = array[i];
        //        [_imageView sd_setImageWithURL:[NSURL URLWithString:name]];
        _imageView.image = [UIImage imageNamed:name];
        //        隐藏指示条
        self.scrollview.showsHorizontalScrollIndicator = NO;
        
        [self.scrollview addSubview:_imageView];
    }
    
    //    2.设置scrollview的滚动范围
    CGFloat contentW = array.count *imageW;
    //不允许在垂直方向上进行滚动
    self.scrollview.contentSize = CGSizeMake(contentW, imageH);
    
    //    3.设置分页
    self.scrollview.pagingEnabled = YES;
    
    //    4.监听scrollview的滚动
    self.scrollview.delegate = self;
    
}

// scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    计算页码
    //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
    CGFloat scrollviewW =  scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollviewW / 2) /  scrollviewW;
    self.pageControl.currentPage = page;
}

// 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
   
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   
}


@end
